package com.bestflashsolutions.components
{
	import com.bestflashsolutions.app.AccessPermission;
	import com.bestflashsolutions.app.AppUserDataInfo;
	import com.bestflashsolutions.components.MyPanel.DragPanel;
	import com.bestflashsolutions.red5Streaming.LiveStreaming;
	import com.bestflashsolutions.red5Streaming.PlayLive;
	
	import flash.display.DisplayObject;
	
	import mx.containers.Canvas;
	import mx.core.Application;
	
	public class ChatWindowLayoutCanvas extends Canvas
	{	
		public static const LAYOUT_TILE:String = "tile";
		public static const LAYOUT_FLOAT:String = "float";
	
		private var _layout:String=LAYOUT_TILE;
		private var oneUp:Boolean;
		public var maxHorSpace:Number;
		public var maxVerSpace:Number;
		
		public const columNumTeacher:Number = 4;
		public var columNumNoTeacher:Number;
		
		private const verGap:int = 5;
		private const horGap:int = 5;
		
		private var popedOutUser:PlayLive;
		private var fromTile:Boolean;
		public function ChatWindowLayoutCanvas(){
			super();
		}
		
		public function tile():void {
			layout = LAYOUT_TILE;
			updateLayOut();
		}
		
		public function float():void {
			fromTile=true;
			layout = LAYOUT_FLOAT;
			updateLayOut();
		}
		
		public function set layout(val:String):void{
			_layout = val
			updateLayOut();
		}
		public function get layout():String{
			return _layout
		}
		
		
		public function updateLayOut(sort:Boolean=true,newPopOutUser:PlayLive=null):void{
			if(!Application.application||!Application.application.objStreaming||!Application.application.objStreaming.objPlayLiveList||!Application.application.objStreaming.objPlayLiveList.lstLiveStream)
				return;
			var liveCtr:int = 0;
			var liveteacher:PlayLive;
			var descendant:PlayLive;
			trace("updateLayOut---------------",stage.stageWidth);
				
			
			if(sort){
				Application.application.objStreaming.objPlayLiveList.lstLiveStream.source.sortOn("joinDate",Array.NUMERIC);
				Application.application.objStreaming.objPlayLiveList.lstLiveStream.refresh();
			}
			
			for(var i:int=0; i<Application.application.objStreaming.objPlayLiveList.lstLiveStream.length; i++){
				var window:PlayLive = Application.application.objStreaming.objPlayLiveList.lstLiveStream[i] as PlayLive;
				/*trace(i,"------->");
				trace("title:",window.title)
				trace("online:",window.available)
				trace("permission:",window.permission);
				trace("joinDate:",window.joinDate);*/
				if(window.permission == AccessPermission.INSTRUCTOR && newPopOutUser==null&&popedOutUser==null){
					//liveteacher = window;
					popedOutUser = newPopOutUser = window;
					if(layout==LAYOUT_TILE&&newPopOutUser==null){
						window.width = renderWidth*.75-horGap;
						window.resWidth = window.cam_width-horGap*2;
						window.fitScaleCam();
					}else{
					
					}
				}else if(newPopOutUser){
					if(popedOutUser&&popedOutUser!=newPopOutUser){
						if(layout==LAYOUT_TILE){
							popedOutUser.width = popedOutUser.cam_width
						}else{
							popedOutUser.width = popedOutUser.resWidth
							popedOutUser.x = popedOutUser.resX;
							popedOutUser.y = popedOutUser.resY;
						}
						popedOutUser.restore();
						window.fitScaleCam()
					}
					
					newPopOutUser.showChatBox();
					newPopOutUser.width = renderWidth*.75-horGap;
					newPopOutUser.fitScaleCam();
					popedOutUser = newPopOutUser;
				}else if(popedOutUser && layout==LAYOUT_TILE){
					popedOutUser.width = renderWidth*.75-horGap;
					popedOutUser.fitScaleCam();
				}
				
			}
			
			
			
			if(fromTile||layout==LAYOUT_TILE){
				
				var studCnt:int = 0;
				columNumNoTeacher = Math.floor(renderWidth/window.cam_width);
				
				for(i=0; i<Application.application.objStreaming.objPlayLiveList.lstLiveStream.length; i++){
					var window:PlayLive = Application.application.objStreaming.objPlayLiveList.lstLiveStream[i] as PlayLive;
					window.enableControl(false);
					if(fromTile)
						window.spaceVerGap=50;
					else
						window.spaceVerGap=30;
					
					window.setStyle("dropShadowEnabled",false);
					
					if(popedOutUser==window){
						window.x = window.resX = horGap;
						window.y = window.resY = verGap;
					}else{
						if(!popedOutUser){
							window.width = window.resWidth =window.cam_width-horGap*2;
							window.fitScaleCam()
							window.validateNow();
							var xPos:int = (studCnt%columNumNoTeacher)*(window.width+horGap);
							var yPos:int = (Math.floor(studCnt/columNumNoTeacher))*(window.height+verGap);
							window.x = window.resX = xPos+horGap;
							window.y = window.resY = yPos+verGap;
						}else{
							window.width = window.resWidth = (renderWidth*.25)-horGap;
							window.fitScaleCam()
							window.validateNow();
							if(studCnt<2){
								xPos = 3*(window.width+horGap);
								yPos = studCnt*(window.height+horGap);
							}else{
								xPos = (studCnt-2)%columNumTeacher*(window.width+horGap);
								yPos = (popedOutUser.height+verGap)+(Math.floor((studCnt-2)/columNumTeacher)*(window.height+verGap));
							}
							if(window.available){
								window.x = window.resX = xPos+horGap;
								window.y = window.resY = yPos+verGap;
								window.isNew = false;
							}else{
								window.x = window.resX = 0;
								window.y = window.resY = 0;
							}
						}
						
						studCnt++;
						
					}
					
					
				}
				if(fromTile){
					fromTile = false;
					updateLayOut();
				}
			}else{
				
				for(i=0; i<Application.application.objStreaming.objPlayLiveList.lstLiveStream.length; i++){
					window = Application.application.objStreaming.objPlayLiveList.lstLiveStream[i] as PlayLive;
					window.enableControl(true);
					window.spaceVerGap=50;
					window.fitScaleCam();
					window.setStyle("dropShadowEnabled",true);
					
					if(newPopOutUser){
						newPopOutUser.x = horGap;
						newPopOutUser.y = verGap;
					}
					if(window.isNew){
						window.width = window.cam_width-horGap*2;
						window.fitScaleCam()
						window.validateNow();
						window.x = window.resX = horGap;
						window.y = window.resY = verGap
						window.isNew = false;
						setChildIndex(window,numChildren-1);
					}
					//}
					if(!window.available){
						window.x = window.resX = 0;
						window.y = window.resY = 0;
					}
					
				}
			}
			
		}
		
		public function get renderWidth(): Number {
			return width-(horGap*2);	
		}
		
		public function get renderHeight(): Number {
			return height;	
		}
		
		override public function addChild(child:DisplayObject):DisplayObject{
			if(child is PlayLive){
				super.addChildAt(child,0)
			}else{
				super.addChild(child)
			}
			
			return child;	
		}
		
		
		
	}
}